<?php
include_once(dirname(__FILE__) . "/config.php");

set_include_path(
get_include_path()
. PATH_SEPARATOR . realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'business')
. PATH_SEPARATOR . realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'data_access')
);

GLOBAL $LOGERRORSET;
$LOGERRORSET = false;
session_start();
GLOBAL $classPathMap;
$classPathMap = array(
	"PHPMailer" => "/lib/email/class.phpmailer.php",
	"SMTP" => "/lib/email/class.smtp.php",
	"Cache" => "/lib/cache.php",
	"RedisCache" => "/lib/RedisCache.php",
	"SessionHandler" => "/lib/SessionHandler.php",
	"UidGenerator" => "/lib/UidGenerator.php",
	"zipfile" => "/lib/zipfile.php"
);
// Carpetas que se incluyen autmaticamente
$folders = array(
		"data_access",
		"business",
		"lib"
);
foreach ($folders as $folder){
	includeFolder($folder);
}

/* Include specific files manually */
$files = array(
"lib/email/mail_functions.php",
"lib/jsonResponse.php",
"lib/logguer.php",
"lib/language.php",
"lib/utilities.php",
);

foreach ($files as $file){
	include_once(PATHROOT. "/" .$file);
}

setLanguage("en");

if (USEFIXEDUSERS && USEOPENFIRE){ 
	/* Ad mock users in openfire if they don't exist */
	Openfire::addMockUSers();
}
$publicPages = array();

if(isset($_REQUEST["f"])){
	$file = $_REQUEST["f"];
	
	//si le pega a un directorio incluyo el index.php si existe
	if (is_dir($file)){
		$file .= (substr($file, -1) == "/") ? "index.php" : "/index.php";
	}
	
	if(file_exists($file)){
		unset($_REQUEST["f"]);
		include_once($file);
	}
}

/**/
function getDirectoryList ($directory)
  {
    $results = array();
    $handler = opendir($directory);
    while ($file = readdir($handler)) {
      if ($file != "." && $file != ".." && $file != ".svn") {
        $results[] = $file;
      }
    }
    closedir($handler);
    return $results;
  }


function includeFolder($folder){
	$files = getDirectoryList(PATHROOT .$folder);
	foreach ($files as $file){
		if(is_dir(PATHROOT . $folder . "/". $file)){
			includeFolder($folder. "/" .$file);
		}else{
			include_once(PATHROOT . $folder . "/". $file);
		}
	}
}

function loader_class($className){
	//echo 'Intentando cargar ', $className, ' via ', __METHOD__, "() </br> ";
	@include_once $className . '.php';
}

function loader_others($className){
	//echo 'Intentando cargar ', $className, ' via ', __METHOD__, "() </br> ";
	GLOBAL $classPathMap;
	if(isset($classPathMap[$className])){
		@include_once dirname(__FILE__) . $classPathMap[$className];
	}
}
?>
